package com.citycloud.ccuap.tc.admin.tools;

import com.citycloud.ccuap.tc.admin.constant.AdminConstant;

import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/***
 * @className SignatureUtils
 * @author wangyj
 * @Date 2019/12/10 9:50
 * @description
 */
public class SignatureUtils {
    
    /**
     * 获取MD5签名
     *
     * @param request 参数
     * @return
     */
    public static String getTestSignatureStr(Long timeStamp, ServletRequest request) {
        String key = "params=";
        List<String> paramKeys = new ArrayList<>();
    
        Map<String, String[]> params = request.getParameterMap();
        paramKeys.add("timeStamp");
    
        for (Map.Entry<String, String[]> entry : params.entrySet()) {
            paramKeys.add(entry.getKey());
        }
        paramKeys.sort(String.CASE_INSENSITIVE_ORDER);
        String paramValues;
        for (String paramKey : paramKeys) {
            if("timeStamp".equals(paramKey)){
                key = key + paramKey + "=" +timeStamp + "&";
            } else if (null != params.get(paramKey) && params.size() > 0) {
                paramValues = Arrays.asList(params.get(paramKey)).toString();
                key = key + paramKey + "=" + paramValues.substring(1, paramValues.length() - 1) + "&";
            } else {
                key = key + paramKey + "=&";
            }
        }
    
        if (key.endsWith(AdminConstant.STR_QUESTION_AMPERSEND)) {
            key = key.substring(0, key.length() - 1);
        }
        return key;
    
    }
    
    
    
}
